package com.behelpful.demo.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @author Alay
 * @date 2021-09-22 22:53
 * @since
 */
@Configuration
public class CoreOriginConfig {

    /**
     * Gateway 上配置 跨域问题
     *
     * @return
     */
    @Bean
    public CorsWebFilter coreWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // * 号表示匹配任意的
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");

        PathPatternParser patternParser = new PathPatternParser();
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(patternParser);
        // ** 代表所有
        source.registerCorsConfiguration("/**", config);

        CorsWebFilter corsWebFilter = new CorsWebFilter(source);

        return corsWebFilter;
    }

}
